package com.taiyi.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taiyi.crm.mapper.ProductMapper;
import com.taiyi.crm.pojo.entity.Product;
import com.taiyi.crm.service.IProductService;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> selectAll() {
        return productMapper.selectAll();
    }

    @Override
    public LayUITableResult selectByPage(Integer page, Integer limit, Product product) {
        //1、使用PageHelper插件设置分页
        PageHelper.startPage(page, limit);
        //2、执行查询
        List<Product> list = productMapper.selectByPage(product);
        //3、使用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        Integer count = (int) pageInfo.getTotal();//得到总数量

        return  LayUITableResult.ok(count, list);
    }

    @Override
    public void insert(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKey(product);
    }
}